package com.tuyue.police.controller.ys;

import com.tuyue.police.biz.IYsDeviceBiz;
import com.tuyue.police.response.CodeImageResponse;
import com.tuyue.police.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by dell on 2018/6/1.
 */

@RestController
@RequestMapping("ys/device")
public class YsDeviceController {

    @Autowired
    private IYsDeviceBiz iYsDeviceBiz;

    /**
     * @Author: 文婧瑶
     * @Description: 添加设备并添加子账户权限
     * @Date: 14:12 2018/6/1
     */
    @PostMapping("addDevice")
    public Result addDevice(@RequestParam(value = "deviceSerial", required = true) String deviceSerial,
                            @RequestParam(value = "validateCode", required = true) String validateCode,
                            @RequestParam(value = "accountId", required = true) String accountId) throws Exception {

        return iYsDeviceBiz.addDeviceAndSetAccess(deviceSerial, validateCode, accountId);
    }

    /**
     * @Author: 文婧瑶
     * @Description: 删除子账户权限并删除设备
     * @Date: 15:51 2018/6/1
     */
    @PostMapping("delDevice")
    public Result delDevice(@RequestParam(value = "deviceSerial", required = true) String deviceSerial,
                            @RequestParam(value = "accountId", required = true) String accountId) {

        return iYsDeviceBiz.delDeviceAndDelAccess(deviceSerial, accountId);
    }

    /**
     * @Author: 文婧瑶
     * @Description: 修改设备
     * @Date: 16:29 2018/6/1
     */
    @PostMapping("update")
    public Result update(@RequestParam(value = "accountId", required = true) String accountId,
                         @RequestParam(value = "deviceSerial", required = true) String deviceSerial,
                         @RequestParam(value = "deviceName", required = true) String deviceName) {

        return iYsDeviceBiz.updateDevice(accountId, deviceSerial, deviceName);
    }

    /**
     * @Author: 文婧瑶
     * @Description: 生成设备配网二维码
     * @Date: 17:05 2018/6/1
     */
    @PostMapping("qrcode")
    public String qrcode(@RequestParam(value = "accountId", required = true) String accountId,
                         @RequestParam(value = "ssid", required = true) String ssid,
                         @RequestParam(value = "password", required = true) String password) {

        Result<CodeImageResponse> qrcode = iYsDeviceBiz.qrcode(accountId, ssid, password);
        String s = "data:image/jpg;base64," + qrcode.getData().getImageData();
        System.out.println(s);
        return s;
    }

    /**
     * @Author: 文婧瑶
     * @Description: 获取摄像头列表
     * @Date: 17:05 2018/6/1
     */
    @PostMapping("list")
    public Result list(@RequestParam(value = "accountId", required = true) String accountId,
                       int pageStart, int pageSize) {

        return iYsDeviceBiz.list(accountId, pageStart, pageSize);
    }

    /**
     * @Author: 文婧瑶
     * @Description: 获取直播地址
     * @Date: 17:05 2018/6/1
     */
    @PostMapping("getBroadcastAddress")
    public Result getBroadcastUrl(@RequestParam(value = "accountId", required = true) String accountId,
                                  @RequestParam(value = "source", required = true) String source) {

        System.out.println(System.currentTimeMillis());
        return iYsDeviceBiz.getBroadcastAddress(accountId, source);
    }

    /**
     * @Author: 文婧瑶
     * @Description: 获取报警消息列表
     * @Date: 17:05 2018/6/1
     */
    @PostMapping("policeList")
    public Result policeList(@RequestParam(value = "accountId", required = true) String accountId,
                             Long startTime, Long endTime, Integer alarmType, Integer status, Integer pageStart, Integer pageSize) {

        return iYsDeviceBiz.policeList(accountId, startTime, endTime, alarmType, status, pageStart, pageSize);
    }

    /**
     * @Author: 文婧瑶
     * @Description: 开启设备视频加密
     * @Date: 8:54 2018/6/4
     */
    @PostMapping("videoEncryptionOn")
    public Result videoEncryptionOn(@RequestParam(value = "accountId", required = true) String accountId,
                                    @RequestParam(value = "deviceSerial", required = true) String deviceSerial) {

        return iYsDeviceBiz.videoEncryptionOn(accountId, deviceSerial);
    }

    /**
     * @Author: 文婧瑶
     * @Description: 关闭设备视频加密
     * @Date: 10:47 2018/6/4
     */
    @PostMapping("videoEncryptionOff")
    public Result videoEncryptionOff(@RequestParam(value = "accountId", required = true) String accountId,
                                     @RequestParam(value = "deviceSerial", required = true) String deviceSerial,
                                     @RequestParam(value = "validateCode", required = true) String validateCode) {

        return iYsDeviceBiz.videoEncryptionOff(accountId, deviceSerial, validateCode);
    }

    /**
     * @Author: 文婧瑶
     * @Description: 设备布撤防
     * @Date: 16:12 2018/6/4
     */
    @PostMapping("setProtection")
    public Result setProtection(@RequestParam(value = "accountId", required = true) String accountId,
                                @RequestParam(value = "deviceSerial", required = true) String deviceSerial,
                                @RequestParam(value = "isDefence", required = true) int isDefence) {

        return iYsDeviceBiz.setProtection(accountId, deviceSerial, isDefence);
    }

    /**
     * @Author: 文婧瑶
     * @Description: 获取单个设备信息
     * @Date: 13:44 2018/6/6
     */
    @PostMapping("deviceInfo")
    public Result deviceInfo(@RequestParam(value = "accountId", required = true) String accountId,
                             @RequestParam(value = "deviceSerial", required = true) String deviceSerial) {

        return iYsDeviceBiz.deviceInfo(accountId, deviceSerial);
    }

}
